home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5221 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  59 lines

  1. Path: cais.cais.com!usaid
  2. From: usaid@cais.cais.com (USAID)
  3. Newsgroups: comp.lang.c
  4. Subject: Question about sscanf
  5. Date: 8 Feb 1996 23:42:18 GMT
  6. Organization: Capital Area Internet Service info@cais.com 703-448-4470
  7. Message-ID: <4fe1oq$kuf@zippy.cais.net>
  8. NNTP-Posting-Host: cais.com
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Here is a fragment of code that I'm having a problem with.  The program 
  12. is bombing on the line containing the call to sscanf.  It assigns to the 
  13. first variable fine, but blows up on the second one.  I have no idea 
  14. what's wrong.
  15.  
  16. int formatFile(void)
  17. {
  18.    int i;
  19.    char line[MAX_LINE_LEN + 1]; 
  20.    char *name; 
  21.    char *value; 
  22.    char *val[MAXREGIONS]; 
  23.    char *delimiter = ", "; 
  24.    FILE *ifp, *ofp; 
  25.  
  26.    /* open input and output files */
  27.    ifp = fopen("/opt/basis/tcs/formdata/test.txt", "r"); 
  28.    ofp = fopen("/opt/basis/tcs/formdata/import.txt", "w"); 
  29.    if ((ifp == NULL) || (ofp == NULL))
  30.       return (0); 
  31.  
  32.    while (fgetline(ifp, line, MAX_LINE_LEN) != -1)
  33.    {
  34.       /* store the name and value in variables */
  35.       if (sscanf(line, "%s %s", name, value) != 2)
  36.       {
  37.          printf("Bad input from sscanf\n");
  38.          return (0); 
  39.       }
  40.         .
  41.         .
  42.         .
  43.     }
  44.           
  45. }
  46.  
  47. The program doesn't even get to the error check; it bombs somewhere in 
  48. sscanf.  the function fgetline is a function I wrote; it parses the file, 
  49. reads each line a character at a time.  It stops at \n or EOF, and sticks 
  50. the line into a null-terminated char array.  It works like it's supposed 
  51. to, in that it gets the first line of the file on the first iteration of 
  52. the while, then bombs at the sscanf.  The data is separated by spaces.  
  53. sscanf assigns to name, but the only thing in value is garbage when I 
  54. check it with the debugger.  My code is similar to examples in a book I 
  55. have.  I'm working in UNIX (Solaris 2.4), in case that helps.
  56.  
  57. Thanks in advance,
  58. Jed Prentice
  59.